// 内容丰富提示框
import { analyzeData } from '@/services/ant-design-pro/highClassWrite.ts';
import { message } from 'antd';
import { nanoid } from 'nanoid';
import React from 'react';
import { connect } from 'umi';
const ContentRich: React.FC<any> = ({
  data,
  scrollToBottom,
  setAILogs,
  toggleHandle,
  highClassHandle,
  dispatch,
}) => {
  const isUpload = data.isUpload || false;
  const material_id = data.material_id || ''; //上传文件的id
  const material_type = data.material_type || ''; //上传文件的id
  //公共更新model的方法
  const updateState = (updateObj: any) => {
    dispatch({
      type: 'highClassWrite/updateState',
      payload: updateObj,
    });
  };

  //点击分析文件
  const handleAnalyzeFile = async () => {
    console.log(data);
    console.log(material_id);

    //添加文件参考方向选择框
    let systemTxtRecover = {
      key: nanoid(),
      type: 'systemTxtRecover',
      spend: 30,
      data: [`好的，小麦正在分析资料`],
    };
    setAILogs((prev: any) => [...prev, systemTxtRecover]);
    updateState({
      robotLoading: true,
    });
    scrollToBottom();
    let body = {
      material_id,
      material_type: material_type === 2 ? '结构资料' : material_type === 3 ? '内容资料' : '',
    };
    let res = await analyzeData(body);
    if (res.code === 0) {
      updateState({
        robotLoading: false,
      });
      //添加文件参考方向选择框
      let analyzeResult = {
        key: nanoid(),
        type: 'analyzeResult',
        data: [
          {
            subject: res?.data?.subject,
            main_body: res?.data?.result,
          },
        ],
      };

      setAILogs((prev: any) => [...prev, analyzeResult]);
    } else {
      message.open({
        type: 'error',
        content: res.msg,
      });
      updateState({
        robotLoading: false,
      });
    }
  };

  //点击找参考资料
  const handleSearch = () => {
    let type = 'findInfo';
    //查找当前的参考资料
    let findInfo = highClassHandle.find((item: any) => item.type === type);
    toggleHandle(findInfo);
  };

  //点击上传参考资料
  const handleUpload = () => {
    let type = 'uploadInfo';
    //查找当前的参考资料
    let findInfo = highClassHandle.find((item: any) => item.type === type);
    toggleHandle(findInfo);
  };

  return (
    <div className="contentRichBox">
      <div className="contentRichTip">参考空间内容越丰富，生成的文章越好。您可以：</div>
      {isUpload && (
        <div className="contentRichList">
          <span className="contentRich-hd">分析上传的资料</span>
          <span
            className="contentRich-bd"
            onClick={() => {
              handleAnalyzeFile();
            }}
          >
            分析资料
          </span>
        </div>
      )}

      <div className="contentRichList">
        <span className="contentRich-hd">小麦帮我找参考资料</span>
        <span className="contentRich-bd" onClick={() => handleSearch()}>
          找参考资料
        </span>
      </div>
      <div className="contentRichList">
        <span className="contentRich-hd">我有更多的参考资料</span>
        <span className="contentRich-bd" onClick={() => handleUpload()}>
          上传参考
        </span>
      </div>
    </div>
  );
};

// export default ContentRich;
export default connect(({ highClassWrite }) => ({ highClassWrite }))(ContentRich);
